/**
 * Created by shichunguang on 16/10/10.
 */
import React, { Component } from 'react';
import {
    AppRegistry,
    StyleSheet,
    Text,
    View,
    ListView,
    Image,
    TouchableOpacity,
    Platform,
    TextInput,
    Navigator,
    AsyncStorage
} from 'react-native';

//Dimentsions
var Dimensions = require('Dimensions');
var Width = Dimensions.get('window').width; //屏宽
var NavigationBar = require('../Main/NXNavigationBar');
var Register = require('../Mine/NXRegister');
var NetUitl = require('../Main/NXFetch');
var getToken = ''
var Login = React.createClass({
    render() {
        return (
            <View style={styles.container}>
                <NavigationBar title="登录" widthWH={Platform.OS == 'ios' ? 15:15} leftBtnName='return_icon' rightBtnName='Share_icon'  leftpress={()=>this.leftBtnAction()} rightPress={()=>this.rightBtnAction()} />

                <TextInput
                    style={styles.inputStyleUser}
                    keyboardType={'number-pad'}
                    placeholder="请输入手机号"
                    clearButtonMode={'always'}
                    ref = 'telephone'
                >
                </TextInput>
                <TextInput
                    style={styles.inputStylePassword}
                    placeholder="请输入密码"
                    password={true}
                    ref ='passwords'
                >
                </TextInput>
                <View style={styles.loginStyle}>
                    <Text style={styles.btnLogin} onPress={()=>this.loginBtnAction()}>登录</Text>
                </View>
                <View style={styles.forgotStyle}>
                    <Text style={styles.btnForgot} onPress={()=>this.forgetBtnAction()}>忘记密码</Text>
                    <Text style={styles.btnNew} onPress={()=>this.newBtnAction()}>没有账号</Text>
                </View>
            </View>
        );
    },
    leftBtnAction(){
        this.props.navigator.pop({

            }
        );
    },
    rightBtnAction(){
        alert('又说那话')
    },
    loginBtnAction(){
        var tel = this.refs.telephone._lastNativeText;
        var password = this.refs.passwords._lastNativeText;
        if((tel == undefined || tel.length != 11)||(password == undefined)||password.length < 6){
            alert('请检查输入的手机号或是密码是否正确')
        }else {
            let formData = new FormData();
            formData.append("accountName", tel);
            formData.append("password", password);
            var fetchOptions = {
                method: 'POST',
                body: formData
            };

            fetch('http://www.pintasty.cn/user/login', fetchOptions)
                .then((response) => {
                    console.log(response.status);
                    if (response.status == 200){
                        var token = ""
                        token =  response.headers.map.token[0];
                        console.log(token)
                        getToken = token
                        //alert(getToken)
                        try {
                            AsyncStorage.setItem(
                                'token',
                                token,
                                (error)=>{
                                    if (error){
                                        alert('存值失败:',error);
                                    }else{
                                        //alert('存值成功!');
                                        this.props.navigator.pop({

                                            }
                                        );
                                    }
                                }
                            );
                        } catch (error){
                            alert('失败'+error);
                        }
                    }
                })
                .done();

        }
    },
    forgetBtnAction(){
        alert('忘记密码了?????')
    },
    newBtnAction(){
        this.props.navigator.push({
            component:Register,
        })

    }

});


var styles = StyleSheet.create({
    container: {
        flex: 1,
        backgroundColor:'white',

    },
    navigatorStyle:{

    },
    inputStyleUser:{
        marginTop:44,
        marginLeft:20,
        marginRight:20,
        //width:Width*3/4,
        height:44,
        borderWidth:1,
        borderRadius:6,
        borderColor:'#e8e8e8'

    },
    inputStylePassword:{
        marginTop:20,
        marginLeft:20,
        marginRight:20,
        borderRadius:6,
        //width:Width*3/4,
        height:44,
        borderWidth:1,
        borderColor:'#e8e8e8'
    },
    loginStyle:{
        marginTop:10,
        marginLeft:22,
        marginRight:22,
        height:44,
        backgroundColor:'red',
        borderRadius:6
    },
    btnLogin:{
        fontSize:20,
        textAlign:'center',
        color:'white',
        marginTop:11
    },
    forgotStyle:{
        marginTop:10,
        marginLeft:22,
        width:Width-44,
        height:16,
        flexDirection:'row',
        justifyContent:'space-between'
    },
    btnForgot:{
        fontSize:16,
        textAlign:'center',
        color:'gray'
    },
    btnNew:{
        fontSize:16,
        textAlign:'center',
        color:'gray'
    }

});

module.exports = Login;
